﻿Public Class CodeBreaker

    Private connectedClient As ConnectedClient
    Private mastermind As Mastermind
    Private activeCodeRow As CodePegRow
    Private activeKeyRow As KeyPegRow

    Private Sub CodeBreaker_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        connectedClient = ConnectForm.MyConnectedClient
        mastermind = ConnectForm.MyMastermind
        activeCodeRow = CodePegRow1
        activeKeyRow = KeyPegRow1

        InitializeCodeBreakerForm()
    End Sub

    Private Sub InitializeCodeBreakerForm()
        Label1.Text = "Waiting for opponents turn"
        Label2.Text = "Score: " + mastermind.GetScore().ToString()
        Button1.Enabled = False
        InitializeCodePegList()
        InitializeCodePegBoard()
        InitializeKeyPegBoard()
    End Sub

    Private Sub InitializeCodePegList()
        CodePegList1.InitializeCodePegs()
    End Sub

    Private Sub InitializeCodePegBoard()
        CodePegRow1.InitializeCodePegRow()
        CodePegRow2.InitializeCodePegRow()
        CodePegRow3.InitializeCodePegRow()
        CodePegRow4.InitializeCodePegRow()
        CodePegRow5.InitializeCodePegRow()
        CodePegRow6.InitializeCodePegRow()
        CodePegRow7.InitializeCodePegRow()
        CodePegRow8.InitializeCodePegRow()
        CodePegRow9.InitializeCodePegRow()
        CodePegRow10.InitializeCodePegRow()
    End Sub

    Private Sub InitializeKeyPegBoard()
        KeyPegRow1.InitializeKeyPegRow()
        KeyPegRow2.InitializeKeyPegRow()
        KeyPegRow3.InitializeKeyPegRow()
        KeyPegRow4.InitializeKeyPegRow()
        KeyPegRow5.InitializeKeyPegRow()
        KeyPegRow6.InitializeKeyPegRow()
        KeyPegRow7.InitializeKeyPegRow()
        KeyPegRow8.InitializeKeyPegRow()
        KeyPegRow9.InitializeKeyPegRow()
        KeyPegRow10.InitializeKeyPegRow()
    End Sub

    Public Sub EnableUI()
        CodePegList1.Enable()

        Dim row As Integer = mastermind.GetRow
        Select Case row
            Case 0
                CodePegRow1.Enable()
                activeCodeRow = CodePegRow1
                activeKeyRow = KeyPegRow1
            Case 1
                CodePegRow2.Enable()
                activeCodeRow = CodePegRow2
                activeKeyRow = KeyPegRow2
            Case 2
                CodePegRow3.Enable()
                activeCodeRow = CodePegRow3
                activeKeyRow = KeyPegRow3
            Case 3
                CodePegRow4.Enable()
                activeCodeRow = CodePegRow4
                activeKeyRow = KeyPegRow4
            Case 4
                CodePegRow5.Enable()
                activeCodeRow = CodePegRow5
                activeKeyRow = KeyPegRow5
            Case 5
                CodePegRow6.Enable()
                activeCodeRow = CodePegRow6
                activeKeyRow = KeyPegRow6
            Case 6
                CodePegRow7.Enable()
                activeCodeRow = CodePegRow7
                activeKeyRow = KeyPegRow7
            Case 7
                CodePegRow8.Enable()
                activeCodeRow = CodePegRow8
                activeKeyRow = KeyPegRow8
            Case 8
                CodePegRow9.Enable()
                activeCodeRow = CodePegRow9
                activeKeyRow = KeyPegRow9
            Case 9
                CodePegRow10.Enable()
                activeCodeRow = CodePegRow10
                activeKeyRow = KeyPegRow10
        End Select

        Label1.Text = "It's your turn"
        Button1.Enabled = True
    End Sub

    Public Sub DisableUI()
        CodePegRow1.Disable()
        CodePegRow2.Disable()
        CodePegRow3.Disable()
        CodePegRow4.Disable()
        CodePegRow5.Disable()
        CodePegRow6.Disable()
        CodePegRow7.Disable()
        CodePegRow8.Disable()
        CodePegRow9.Disable()
        CodePegRow10.Disable()

        CodePegList1.Disable()

        Button1.Enabled = False
    End Sub

    Public Sub Decode(ByVal data As String)
        mastermind.Decode(data, mastermind.GetKeyPegBoard)

        Dim row As Integer = mastermind.GetRow
        Dim array(,) As String = mastermind.GetKeyPegBoard

        For i As Integer = 0 To array.GetUpperBound(1)
            activeKeyRow.SetColor(i, array(row, i))
        Next

        EnableUI()
    End Sub

    Public Sub NextRound()
        MsgBox("Get ready for round 2")
        mastermind.IncreaseActiveGame()
        mastermind.ResetGame()
        Me.Hide()
        CodeMaker.Show()
        CodeMaker.EnableUI()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim validation As Boolean = False

        mastermind.SetCodeRow(mastermind.GetRow, activeCodeRow.GetCodeRow)

        validation = mastermind.ValidateGuess()

        If Not validation Then
            Label1.Text = "All slots of code row must be filled before proceeding"
        Else
            Dim codeRow() As String = activeCodeRow.GetCodeRow
            Dim message As String = mastermind.Encode(codeRow)
            connectedClient.SendData(message)
            Label1.Text = "Waiting for opponents turn"
            DisableUI()
            mastermind.IncreaseRow()
        End If
    End Sub

    Private Sub CodeBreaker_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        connectedClient.SendData("Disconnected")
        connectedClient.ShutDownClient()

        ConnectForm.Close()
    End Sub

End Class